package com.cheyun.netsalev3.view.defeataudit


import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.Row
import com.cheyun.netsalev3.databinding.FragmentDefeatAuditActivityBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.DefeatAuditActivityViewMoldel


class DefeatAuditActivityFragment : BaseFragment<FragmentDefeatAuditActivityBinding>() {

    override fun initContentView(): Int {
        return R.layout.fragment_defeat_audit_activity
    }

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            return ViewModelProviders.of(this).get(DefeatAuditActivityViewMoldel::class.java)
        }
        return null
    }

    lateinit var adapter: MyPagedListAdapter<Row>
    override fun initView() {

        adapter = MyPagedListAdapter(R.layout.defeat_audit_adpter_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.viewModel?.pagedlist!![nkey]?.observe(this, Observer {
            adapter.submitList(it)
        })
        initSwipeToRefresh()
    }

    private fun initSwipeToRefresh() {
        binding.viewModel?.hashMap!![nkey]?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter.setNetworkState(it)
        })
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh(nkey)

        }
    }

    override fun initData() {
    }
    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }
    private var nkey: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            nkey = it.getString("nkey")
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(nkey: String) =
                DefeatAuditActivityFragment().apply {
                    arguments = Bundle().apply {
                        putString("nkey", nkey)
                    }
                }
    }


}
